/**
 * f(0) = 0a + b + 2c + 3d
 * f(1) = a + 2b + 3c + 0d  = f(0) + (a + b + c + d) - 4d
 * f(2) = 2a + 3b + 0c + d = f(1) + (a + b + c + d) - 4c
 * @param {number[]} nums
 * @return {number}
 */
export var maxRotateFunction = function (nums) {
  if (nums.lenght < 2) return 0
  let [curr, sum] = [0, 0]
  for (let i = 0; i < nums.length; i++) {
    sum += nums[i]
    curr += i * nums[i]
  }

  let ans = curr
  for (let i = 1; i < nums.length; i++) {
    curr += sum - nums.length * nums[nums.length - i]
    ans = Math.max(ans, curr)
  }
  return ans
}

/**
 * 暴力，滑动窗口 （能过过本地测试，但数据量大时，提交会超时）
 * @param {number[]} nums
 * @return {number}
 */
export var maxRotateFunction2 = function (nums) {
  if (nums.lenght < 2) return 0
  const n = nums.length
  nums = nums.concat(nums)
  let ans = -Infinity
  let left = 1
  let right = n
  while (right < n * 2) {
    let sum = 0
    for (let i = left; i < right; i++) {
      sum += nums[i] * (i - left + 1)
    }
    ans = Math.max(ans, sum)
    left++
    right++
  }
  return ans
}
